package com.ai.platform.config;

import com.ai.platform.core.datasource.DatasourceEnum;
import com.ai.platform.core.datasource.CommonSourceProperties;
import com.ai.platform.core.datasource.DynamicDataSource;
import com.ai.platform.core.datasource.BusinessSourceProperties;
import com.ai.platform.scope.DataScopeInterceptor;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.SQLException;
import java.util.HashMap;

/**
 * MybatisPlus配置
 * Created by yeyingsheng on 2018/3/18.
 */
@Configuration
@EnableTransactionManagement(order = 2)//由于引入多数据源，所以让spring事务的aop要在多数据源切换aop的后面
@MapperScan(basePackages = {"com.ai.platform.**.mapper*"})
public class MybatisPlusConfig {

    @Autowired
    CommonSourceProperties druidProperties;

    @Autowired
    BusinessSourceProperties businessSourceProperties;

    /**
     * Business数据源
     */
    private DruidDataSource dataSourceBusiness() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        businessSourceProperties.config(dataSource);
        return dataSource;
    }

    /**
     * Common数据源
     * @return
     */
    private DruidDataSource dataSourceCommon() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    /**
     * 单数据源连接池配置
     */
    @Bean
    @ConditionalOnProperty(prefix = "spring", name = "multi-datasource-open", havingValue = "false")
    public DruidDataSource singleDatasource() {
        return dataSourceCommon();
    }

    /**
     * 多数据源连接池配置
     */
    @Bean
    @ConditionalOnProperty(prefix = "spring", name = "multi-datasource-open", havingValue = "true")
    public DynamicDataSource mutiDataSource() {
        DruidDataSource dataSourceCommon = dataSourceCommon();
        DruidDataSource dataSourceBusiness = dataSourceBusiness();
        try {
            dataSourceCommon.init();
            dataSourceBusiness.init();
        } catch (SQLException sql) {
            sql.printStackTrace();
        }
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        HashMap<Object, Object> hashMap = new HashMap();
        hashMap.put(DatasourceEnum.DATA_SOURCE_COMMON, dataSourceCommon);
        hashMap.put(DatasourceEnum.DATA_SOURCE_BUSINESS, dataSourceBusiness);
        dynamicDataSource.setTargetDataSources(hashMap);
        dynamicDataSource.setDefaultTargetDataSource(dataSourceCommon);
        return dynamicDataSource;
    }

    /**
     * 数据范围mybatis插件
     */
    @Bean
    public DataScopeInterceptor dataScopeInterceptor() {
        return new DataScopeInterceptor();
    }
}
